var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += nowYear < 2000 ? 1900 : 0; //

var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastMonth = lastMonthDate.getMonth();
export default {
  //格式化日期
  formatDate(originVal, type) {
    if (!originVal) return "";
    const dt = new Date(originVal);
    const y = dt.getFullYear();
    const m = (dt.getMonth() + 1 + "").padStart(2, "0");
    const d = (dt.getDate() + "").padStart(2, "0");
    const hh = (dt.getHours() + "").padStart(2, "0");
    const mm = (dt.getMinutes() + "").padStart(2, "0");
    const ss = (dt.getSeconds() + "").padStart(2, "0");
    let dateStr = "";
    switch (type) {
      // 年月日 时分秒
      case 1:
        dateStr = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        break;
      // 年月日 时分
      case 2:
        dateStr = `${y}-${m}-${d} ${hh}:${mm}`;
        break;
      // 年月日
      case 3:
        dateStr = `${y}-${m}-${d}`;
        break;
      // 默认 年月日 时分秒
      default:
        dateStr = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    }
    return dateStr;
  },
  //获得某月的天数
  getMonthDays(myMonth) {
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  },

  //获得本季度的开端月份
  getQuarterStartMonth() {
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  },

  //获得本周的开端日期
  getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
    return weekStartDate.getTime();
  },

  //获得本周的停止日期
  getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
    return weekEndDate.getTime();
  },

  //获得本月的开端日期
  getMonthStartDate() {
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return monthStartDate.getTime();
  },

  //获得本月的停止日期
  getMonthEndDate() {
    var monthEndDate = new Date(nowYear, nowMonth, this.getMonthDays(nowMonth));
    return monthEndDate.getTime();
  },

  //获得上月开端时候
  getLastMonthStartDate() {
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return lastMonthStartDate.getTime();
  },

  //获得上月停止时候
  getLastMonthEndDate() {
    var lastMonthEndDate = new Date(
      nowYear,
      lastMonth,
      this.getMonthDays(lastMonth)
    );
    return lastMonthEndDate.getTime();
  },

  //获得本季度的开端日期
  getQuarterStartDate() {
    var quarterStartDate = new Date(nowYear, this.getQuarterStartMonth(), 1);
    return quarterStartDate.getTime();
  },

  //或的本季度的停止日期
  getQuarterEndDate() {
    var quarterEndMonth = this.getQuarterStartMonth() + 2;
    var quarterStartDate = new Date(
      nowYear,
      quarterEndMonth,
      this.getMonthDays(quarterEndMonth)
    );
    return quarterStartDate.getTime();
  },

  // 获得本年开始日期
  getYearStartDate() {
    var yearStartDate = new Date(nowYear, 0, 1);
    return yearStartDate.getTime();
  },
  // 获得本年结束日期
  getYearEndDate() {
    var yearEndDate = new Date(nowYear, 11, 1);
    return yearEndDate.getTime();
  },
  // 根据时间戳算当天的 0点0分0秒 的时间戳
  getStartTime(timer) {
    timer = new Date(timer).toLocaleDateString();
    timer = new Date(timer).getTime();
    return timer;
  },
  // 根据时间戳算当天的 23点59分59秒 的时间戳
  getEndTime(timer) {
    timer = this.getStartTime(timer);
    timer += 24 * 60 * 60 * 1000 - 1;
    return timer;
  },
  // 获得当前时间戳
  getNowDate() {
    return Date.now();
  }
};
